package com.xh.audit.controller.project;

import com.mybatisflex.core.paginate.Page;
import com.xh.audit.entity.project.Project;
import com.xh.audit.service.ProjectService;
import com.xh.audit.vo.webform.ProjectQuery;
import com.xh.common.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 项目立项 前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-10-19
 */
@RestController
@RequestMapping("/project")
@RequiredArgsConstructor
@Validated
public class ProjectController {

    private final ProjectService projectService;

    @PostMapping("/save")
    public R<Project> save(@RequestBody
                           @Validated
                                   Project tosave) {
        boolean saveFlag = projectService.saveOrUpdate(tosave);

        if (saveFlag) {
            return R.success(projectService.getById(tosave.getId()));
        }
        return R.failed("项目立项:保存失败");
    }

    @PostMapping("/queryByPage")
    public R<Page<Project>> query(@RequestBody
                                  @Validated ProjectQuery queryPage) {
        Page<Project> page1 = projectService.page(queryPage.getPageInfo(), queryPage.queryWrapperBuild());
        return R.success(page1);
    }

    @PostMapping("/info/{id}")
    public R<Project> info(@PathVariable
                           @Validated
                           @NotNull(message = "id不能空")
                                   Integer id) {

        Project byId = projectService.getById(id);
        return R.success(byId);
    }

    /**
     * List<Integer> 集合验证 必须在类上也加 @Validated
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public R<Project> del(@RequestBody
                          @Validated
                          @NotEmpty(message = "ids 集合不能空")
                                  List<Integer> ids) {
        projectService.removeByIds(ids);
        return R.success("删除成功");
    }

}
